package com.qf.security.service;

import com.qf.entity.SysUserInfo;
import com.qf.pojo.BaseUser;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @Author ztf
 * @Date 20:56 2022/1/10
 **/
@Service
public class MyUserDetailService implements UserDetailsService {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUserInfo user = userService.findUserByUserName(username);
        if (user == null) {
            throw new UsernameNotFoundException("用户名不存在");
        }
        //用户权限
        ArrayList<GrantedAuthority> list = new ArrayList<>();
        return new BaseUser(user.getuId(), username, user.getPassword(), list);
    }
}
